import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../views/home/index.vue";
//这里的import 不需path的匹配就能引入进来
const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/mine",
    name: "Mine",
    component: function () {
      return import(/* webpackChunkName: "Mine" */ "../views/my/Mine.vue");
    },
    beforeEnter: (to, from, next) => {
      let isLogin = sessionStorage.getItem("token");
      if (!isLogin) {
        router.push("/login?redirct=" + to.fullPath);
      } else {
        next();
      }
    },
  },

  {
    path: "/detail",
    name: "Detail",
    component: function () {
      return import(/* webpackChunkName: "Detail" */ "../views/Detail.vue");
    },
  },
  {
    path: "/login",
    name: "Login",
    component: function () {
      return import(/* webpackChunkName: "Login" */ "../views/Login.vue");
    },
  },
  {
    path: "/test",
    name: "Test",
    component: function () {
      return import(/* webpackChunkName: "Test" */ "../views/Test.vue");
    },
  },
  {
    path: "/cart",
    name: "Cart",
    component: function () {
      return import(/* webpackChunkName: "Test" */ "../views/Cart.vue");
    },
    beforeEnter: (to, from, next) => {
      let isLogin = sessionStorage.getItem("token");
      if (!isLogin) {
        router.push("/login?redirct=" + to.fullPath);
      } else {
        next();
      }
    },
  },
  {
    path: "/order",
    name: "Order",
    component: function () {
      return import(/* webpackChunkName: "Test" */ "../views/Order.vue");
    },
    beforeEnter: (to, from, next) => {
      let isLogin = sessionStorage.getItem("token");
      if (!isLogin) {
        router.push("/login?redirct=" + to.fullPath);
      } else {
        next();
      }
    },
  },
  {
    path: "/address",
    name: "Address",
    component: function () {
      return import(
        /* webpackChunkName: "Test" */ "../views/my/location/Address.vue"
      );
    },
  },
  {
    path: "/changeAddress",
    name: "ChangeAddress",
    component: function () {
      return import(
        /* webpackChunkName: "Test" */ "../views/my/location/ChangeAddress.vue"
      );
    },
  },
  {
    path: "/newAddress",
    name: "NewAddress",
    component: function () {
      return import(
        /* webpackChunkName: "Test" */ "../views/my/location/NewAddress.vue"
      );
    },
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // return 期望滚动到哪个的位置
    return {
      top: 0,
    };
  },
});

export default router;
